package com.ruoyi.framework.web.service;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Mylogin;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static org.springframework.security.config.Elements.JWT;
@Component
public class MyTokenService {
    private static final String SING = "resumewebsecrect";
    public  String mylogincreatedToken(String loginrole,Long userid){
       //用登陆身份和在对应表中的id唯一生成token
        Map<String,Object>claims = new HashMap<>();
        claims.put("loginrole",loginrole);
        claims.put("roleid",userid);
        String token = Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS512,SING).compact();
        System.out.println("CreatedToken="+token);
        return token;

    }
    public Claims myparseToken(String token){

        return Jwts.parser()
                .setSigningKey(SING)
                .parseClaimsJws(token)
                .getBody();
    }

    public String getloginrole(String token){
        return myparseToken(token).get("loginrole",String.class);

    }
    public Long getloginid(String token){
        return myparseToken(token).get("roleid", Long.class);
    }

    /**
     * 处理&链接的字符串放入map中,正确解析url
     * a=1&b=1=>a:1,b:1
     * @param str
     * @return
     */
    public Map<String,String>doparse(String str) throws UnsupportedEncodingException {
        Map<String, String> map = new HashMap<>();

        String[] pairs = str.split("&");
        for (String pair : pairs) {
            String[] keyValue = pair.split("=");
            if (keyValue.length == 2) {
                keyValue[1]= URLDecoder.decode(keyValue[1],"UTF-8");
                map.put(keyValue[0], keyValue[1]);
            }
        }

        // 打印Map中的键值对
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " = " + entry.getValue());
        }
        return map;
    }
    /**
     * String转date yyyy-mm-dd
     */
    public Date str2date(String time) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date newTime = format.parse(time);
        return newTime;
    }
    /**
     * Str2bytes[]
     */
    public byte[] str2bytes(String strfile) throws UnsupportedEncodingException {
        return toString().getBytes(strfile);
    }
    public String bytes2str(byte[] file){
        //return new String(file, StandardCharsets.UTF_8);
        return new String(file);
    }
}
